package fem2.element;

import inf.jlinalg.Array2DMatrix;
import inf.jlinalg.IMatrix;
import math2.MathUtilities;

import org.junit.Assert;
import org.junit.Test;

import fem2.Edge;
import fem2.Element;
import fem2.Node;
import fem2.element.TrussElement;
import fem2.enu.State;
import fem2.material.StVenantKirchhoffMM;

public class TrussElementTest {

	public Element createElement() {
		Node n1 = new Node(0.0);
		Node n2 = new Node(1.0);
		Edge e = new Edge(n1, n2);
		n1.setNumberOfFields(1);
		n2.setNumberOfFields(1);
		e.setNumberOfFields(1);
		StVenantKirchhoffMM mm = new StVenantKirchhoffMM(2.0, 0.3, 1, 1, State.ONE_DIMENSIONAL);
		Element p = new TrussElement(e, mm);
		return p;
	}

	public Element createElement2() {
		Node n1 = new Node(0.0, 0.0);
		Node n2 = new Node(0.0, 1.0);
		Edge e = new Edge(n1, n2);
		n1.setNumberOfFields(2);
		n2.setNumberOfFields(2);
		e.setNumberOfFields(2);
		StVenantKirchhoffMM mm = new StVenantKirchhoffMM(2.0, 0.3, 1, 1, State.ONE_DIMENSIONAL);
		Element p = new TrussElement(e, mm);
		return p;
	}

	@Test
	public void testKe() {
		Element e = createElement();
		e.Initialize();
		IMatrix ke = new Array2DMatrix(2, 2);

		e.computeKte(ke);
		double[][] keRef = new double[][] { { 2.0, -2.0 }, { -2.0, 2.0 } };

		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < 2; j++) {
				Assert.assertEquals(keRef[i][j], ke.get(i, j), MathUtilities.tol14);
			}
		}
	}

	// @Test
	// public void testKe2() {
	// Element e = createElement2();
	// e.Initialize();
	// IMatrix ke = new Array2DMatrix(4, 4);
	//
	// e.computeKte(ke);
	// System.out.println(MatrixFormat.format(ke));
	// }
}
